<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          clearable
          placeholder="地址查询"
          auto-top
          @change="getData()"
        ></select-address>
      </el-form-item>
      <!-- <el-form-item>
        <c-select
          :options="enumStatus"
          v-model="selectStatus"
          clearable
          placeholder="贷后状态"
          @change="handleStatusChange"
        ></c-select>
      </el-form-item> -->
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>
    <postloan-file :visible.sync="reportVisible" :related-data="fileData"></postloan-file>
    <attach-cmpt :visible.sync="attachVisible" :customer-related-id="attachRelatedId"></attach-cmpt>
    <repay-dialog
      :visible.sync="replayVisible"
      :query-id="queryId"
      @success="getData()"
    ></repay-dialog>
    <level-history :visible.sync="levelVisible" :query-id="levelQueryId"></level-history>

    <el-dialog
      :visible.sync="registerVisible"
      append-to-body
      :close-on-click-modal="false"
      title="检查登记"
      width="800px"
    >
      <after-register :related-id="registerId"></after-register>
    </el-dialog>

    <el-dialog
      :visible.sync="checkVisible"
      append-to-body
      :close-on-click-modal="false"
      title="检查报告"
      width="800px"
    >
      <after-check-report :query-id="checkId"></after-check-report>
    </el-dialog>
  </div>
</template>

<script>
import { dateFormat } from '@vimi/utils-tool'
import { enumGuatype, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/county/unionProvince/Approve'
import baseMixins from '@/credit/views/city/bus/normal/mixins'

import SelectAddress from '@/components/select/SelectAddress'
// import LetterView from '@/credit/views/county/mission/components/LetterView'
// import RegisterLoanNotice from '@/credit/views/county/mission/RegisterLoanNotice'
import PostloanFile from '@/credit/views/county/unionProvince/components/loanAfter/PostloanFile'
import AttachCmpt from '@/credit/views/city/program/Attach'
import RepayDialog from '@/credit/views/city/self/after/repay/RepayDialog'
import LevelHistory from '@/credit/views/city/self/after/level/LevelHistory'
import AfterRegister from '@/credit/views/city/self/after/AfterRegister'
import AfterCheckReport from '@/credit/views/city/self/after/report/Index'
// import AttachView from '@/credit/components/AttachView'
export const DATE_EXPIRE = 60

export default {
  components: {
    LevelHistory,
    SelectAddress,
    // LetterView,
    // RegisterLoanNotice,
    AttachCmpt,
    // AttachView
    PostloanFile,
    RepayDialog,
    AfterRegister,
    AfterCheckReport,
  },
  mixins: [baseMixins],
  props: {},
  data() {
    return {
      attachVisible: false,
      attachRelatedId: '',
      params: {
        index: 1,
        size: 10,
        customer_name: '',
        corp_name: '',
        address_code: '',
        'confuse_status.neq': 3,
        'loan_approval_status.eq': 3,
        'load_beh_status.eq': 1,
        'gua_type.eq': '',
        'channel_type.eq': '',
        // subKey: 'entrust_contract.customer_related_id',
      },
      order: {
        N1is_history: 'ASC',
        N2create_time: 'DESC',
      },
      useRegisterTable: true,
      tableColumns: [
        ...baseTableColumns,
        { attrs: { label: '放款金额(万元)', prop: 'reply_funds', align: 'center' } },
        { attrs: { label: '放款时间', prop: 'startDate', align: 'center', width: 100 } },
        { attrs: { label: '到期时间', prop: 'endDate', align: 'center', width: 100 } },
        {
          attrs: { label: '放款银行', prop: 'bank_name', align: 'center' },
          process: v => v || '-',
        },
        { attrs: { label: '在保余额(万元)', prop: 'left_funds', align: 'center' } },
        // { attrs: { label: '审批模式', prop: 'processLabel', width: 80, align: 'center' } },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        { attrs: { label: '业务模式', prop: 'guaType', width: 100, align: 'center' } },
        {
          attrs: { label: '操作', minWidth: 130 },
          operations: [
            {
              content: '五级分类',
              icon: 'fa fa-sort-amount-desc',
              event: this.handlerLevel,
            },
            {
              content: '还款跟踪',
              icon: 'fa fa-credit-card',
              event: this.handlerRepay,
            },
            {
              content: '检查登记',
              icon: 'fa fa-edit',
              event: this.handlerRegister,
            },
            {
              content: '保后报告',
              icon: 'fa fa-file-text-o',
              event: this.handlerCheck,
            },
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.hanleView,
            },
          ],
        },
      ],
      dateFmt: 'yyyy-MM-dd',

      activeName: 'letter',

      replayVisible: false,
      queryId: '',
      reportVisible: false,
      fileData: {},

      // enumStatus: [
      //   { label: '在保项目', value: 0 },
      //   { label: '即将到期', value: 1 },
      //   { label: '解保项目', value: 2 },
      // ],
      enumParams: {
        0: {
          'load_beh_status.neq': 6,
        },
        1: {
          'load_beh_status.neq': 6,
          'load_end_date.lte': dateFormat(+new Date() + DATE_EXPIRE * 8.64e7, 'yyyy-MM-dd'),
        },
        2: {
          'load_beh_status.eq': 6,
        },
      },
      selectStatus: '',

      levelQueryId: '',
      levelVisible: false,

      registerVisible: false,
      registerId: '',

      checkVisible: false,
      checkId: '',
    }
  },
  computed: {},
  watch: {},
  created() {
    this.params['service_id.eq'] = this.clientId
    this.getData()
  },
  mounted() {},
  methods: {
    getParams() {
      return this.params
    },
    handlerCheck(row) {
      this.checkVisible = true
      this.checkId = row.customer_related_id || ''
    },
    handlerRegister(row) {
      this.registerVisible = true
      this.registerId = row.customer_related_id || ''
    },
    handlerLevel(row) {
      this.levelVisible = true
      this.levelQueryId = row.customer_related_id || ''
    },
    hanleAttach(row) {
      this.attachVisible = true
      this.attachRelatedId = row.customer_related_id || ''
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.endDate = this.$utils.dateFormat(item.load_end_date, this.dateFmt)
      item.startDate = this.$utils.dateFormat(item.load_date, this.dateFmt)
      item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`
      item.processLabel = enumProcessStatus[+item.process_status] || ''

      return item
    },
    handlerRepay(row = {}) {
      this.queryId = row.customer_related_id || ''
      this.replayVisible = true
    },
    hanleView(row = {}) {
      this.fileData = row
      this.reportVisible = true
    },
    beforeClose() {
      this.reportVisible = false
      setTimeout(() => (this.activeName = 'letter'), 200)
    },
    // handleStatusChange() {
    //   const stautsParams = this.enumParams[this.selectStatus] || {}
    //   const resetParams = Object.keys(this.enumParams).reduce((prev, curr) => {
    //     for (const key in this.enumParams[curr]) {
    //       prev[key] = ''
    //     }
    //     return prev
    //   }, {})
    //   this.params = {
    //     ...this.params,
    //     ...resetParams,
    //     ...stautsParams,
    //   }
    //   this.getData()
    // },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
